<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.credithc.pay.data.dao.${prefix}.${modelName}Mapper">

    <resultMap id="baseResultMap" type="com.credithc.pay.data.entity.${prefix}.${modelName}">
        <#list columnInfoList as col>
            <#if col.name == 'id'>
                <id column="id" property="id" jdbcType="BIGINT"/>
            </#if>
            <#if col.name != 'id'>
                <result column="${col.name}" jdbcType="${col.jdbcType}" property="${col.modelColumnName}"/>
            </#if>
        </#list>
    </resultMap>

    <sql id="basicColumns">
        <#list columnInfoList as col>
            ${modelNameLowerCamel}.${col.name}<#if col_has_next>,</#if>
        </#list>
    </sql>

    <sql id="conditionWhere">
        <where>
            <#list columnInfoList as col>
                <#if col.javaType == 'String'>
                    <if test="${col.modelColumnName} != null and ${col.modelColumnName} != ''">
                        AND ${modelNameLowerCamel}.${col.name} = ${r'#'}{${col.modelColumnName},jdbcType=${col.jdbcType}
                        }
                    </if>
                </#if>
                <#if col.javaType != 'String'>
                    <if test="${col.modelColumnName} != null">
                        AND ${modelNameLowerCamel}.${col.name} = ${r'#'}{${col.modelColumnName},jdbcType=${col.jdbcType}
                        }
                    </if>
                </#if>
            </#list>
        </where>
    </sql>
    <insert id="insert" parameterType="com.credithc.pay.data.entity.${prefix}.${modelName}" useGeneratedKeys="true"
            keyProperty="id">
        INSERT INTO ${tableName}(
        <#assign sysFields=["id"]>
        <#list columnInfoList as col>
            <#if !sysFields?seq_contains(col.name)>
                ${col.name}<#if col_has_next>,</#if>
            </#if>
        </#list>
        )
        VALUES (
        <#list columnInfoList as col>
            <#if !sysFields?seq_contains(col.name)>
                ${r'#'}{${col.modelColumnName},jdbcType=${col.jdbcType}}<#if col_has_next>,</#if>
            </#if>
        </#list>
        )
    </insert>

    <delete id="deleteByPrimaryKey">
        delete from ${tableName} WHERE id = ${r'#'}{id,jdbcType=BIGINT}
    </delete>

    <update id="updateByPrimaryKeySelective" parameterType="com.credithc.pay.data.entity.${prefix}.${modelName}">
        UPDATE ${tableName}
        <set>
            <#assign sysUpdateFields=["id"]>
            <#list columnInfoList as col>
                <#if !sysUpdateFields?seq_contains(col.name)>
                    <#if col.javaType == 'String'>
                        <if test="${col.modelColumnName} != null and ${col.modelColumnName} != ''">
                            ${col.name} = ${r'#'}{${col.modelColumnName}},
                        </if>
                    </#if>
                    <#if col.javaType != 'String'>
                        <if test="${col.modelColumnName} != null">
                            ${col.name} = ${r'#'}{${col.modelColumnName}},
                        </if>
                    </#if>

                </#if>
            </#list>
        </set>
        WHERE id = ${r'#{id}'}
    </update>

    <select id="selectByPrimaryKey" resultType="com.credithc.pay.data.entity.${prefix}.${modelName}">
        SELECT
        <include refid="basicColumns"/>
        FROM ${tableName} ${modelNameLowerCamel}
        WHERE ${modelNameLowerCamel}.id = ${r'#'}{id,jdbcType=BIGINT}
    </select>

    <select id="selectCount" parameterType="com.credithc.pay.data.entity.${prefix}.${modelName}"
            resultType="java.lang.Integer">
        SELECT
        count(*) as c
        FROM ${tableName} ${modelNameLowerCamel}
        <include refid="conditionWhere"/>
    </select>

    <select id="selectList" parameterType="com.credithc.pay.data.entity.${prefix}.${modelName}"
            resultMap="baseResultMap">
        SELECT
        <include refid="basicColumns"/>
        FROM ${tableName} ${modelNameLowerCamel}
        <include refid="conditionWhere"/>
        <include refid="base.orderAndPage"/>
    </select>
</mapper>
